package com.fht.spring_boot_study.model.test.controller;

import com.fht.spring_boot_study.aspect.MyAnnotation;
import com.fht.spring_boot_study.model.common.entity.ResultEntity;
import com.fht.spring_boot_study.model.common.entity.SearchBean;
import com.fht.spring_boot_study.model.test.entity.City;
import com.fht.spring_boot_study.model.test.service.CityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     * localhost/api/city  -------post
     *
     * {"cityName":"dreamCity", "localCityName":"梦想城市", "countryId":522,
     *       "district":"sichuan", "population":1780000}
     */
    @PostMapping(value = "/city",consumes = "application/json")
    public ResultEntity<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }

    /**
     * localhost/api/city  -------put
     * cityId=2258,cityName=aaa,localCityName=bbb
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public ResultEntity<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);

    }

    /**
     * localhost/api/city/2257  ----delete
     */
    @DeleteMapping("/city/{cityId}")
    public ResultEntity<Object> deleteByCityId(@PathVariable int cityId){
        return cityService.deleteCityByCityId(cityId);
    }

    /**
     *localhost/api/city/2258  ----get
     */
    @GetMapping("/city/{cityId}")
    @MyAnnotation
    public City getCityByCityId(@PathVariable int cityId){
        return cityService.getCityByCityId(cityId);
    }

    /**
     * localhost/api/cities/522  ----get
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     *localhost/api/cities  ----post
     *  {"currentPage":1, "pageSize":5, "orderBy":"city_id",
     *      "direction":"desc", "keyWord": "ch"}
     */
    @PostMapping(value = "/cities",consumes ="application/json" )
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean){
        return cityService.getCitiesBySearchBean(searchBean);
    }
}
